*	Program ORDERPLOT
*
*	--program to create an orderplot in Stata using the output from dynsimpie. 
*	--Andy Philips
*	--Last updated: 12/08/16
* -----------------------------------------------------------------------------

cap program drop orderplot
program define orderplot
syntax , file(string) basetime(numlist) shocktime(numlist) endtime(numlist) ///
 [cat1(string) cat2(string) cat3(string) cat4(string) ///
 cat5(string) cat6(string) cat7(string) cat8(string) cat9(string)		   ///
 cat10(string) cat11(string) cat12(string) saving(string) goptions(string) ///
 legend alternative]

version 10.1
preserve

use "`file'", clear

* keep only the basetime, shocktime, and endtime:
keep if time == `basetime' | time == `shocktime' | time == `endtime'
sort time

* how many categories are there? 
if "`cat12'" != ""	{
	loc totcat = 12
}
else if "`cat11'" != ""	{
	loc totcat = 11
}
else if "`cat10'" != ""	{
	loc totcat = 10
}
else if "`cat9'" != ""	{
	loc totcat = 9
}
else if "`cat8'" != ""	{
	loc totcat = 8
}
else if "`cat7'" != ""	{
	loc totcat = 7
}
else if "`cat6'" != ""	{
	loc totcat = 6
}
else if "`cat5'" != ""	{
	loc totcat = 5
}
else if "`cat4'" != ""	{
	loc totcat = 4
}
else if "`cat3'" != ""	{
	loc totcat = 3
}
else if "`cat2'" != ""	{
	loc totcat = 2
}
else 	{
	loc totcat = 1
}

gen placement = _n
set obs 5 												// for pad
gen pad = .												// gen pad
replace placement = 0 in 4
replace placement = 4 in 5

loc spikes
loc texts
loc legends
if "`alternative'" != ""	{
	loc mids "connected mid1 placement, lcolor(black)"
}
else	{
	loc mids "connected mid1 placement, mlabel(midlab1) mlabposition(5) lcolor(black)"
}
forv i = 1/`totcat'	{
	su mid`i' if placement == 1
	scalar nameplace = r(mean)
	loc nameplace`i' = round(nameplace + nameplace/25, 0.001)
	gen midlab`i' = round(mid`i', 0.01)
	if "`i'" > "1"	{
		if "`alternative'" != ""	{
			loc mids `"`mids' || connected mid`i' placement, lcolor(black) "'
		}
		else	{
			loc mids `"`mids' || connected mid`i' placement, mlabel(midlab`i') mlabposition(5) lcolor(black) "'
		}
	} 
	if "`legend'" != ""	{
		loc legends `"`legends' `i' "`cat`i''" "'
	}
	else	{
		loc legends "off"
		loc texts `"`texts' text(`nameplace`i'' 1 "`cat`i''", placement(11) size(vsmall)) "'
	}
	loc spikes `"`spikes' || rspike var`i'_pie_ul_ var`i'_pie_ll_ placement, lcolor(black) "'	
}

if "`legend'" != ""	{
	loc leg  "order(`legends') size(small)"
}
else	{
	loc leg "off"
}

* Make the graphs:
if "`alternative'" != ""	{
	twoway `mids' `spikes', legend(`leg') xtitle("") ysize(3) xsize(2) yline(0(.25).50, lcolor(gs15) lpattern(solid)) ylabel(0(.25).50, ticks tlength(tiny) tlcolor(black)) xlabel( 1 "Means" 2 " Short-Run" 3 "Long-Run")   plotregion(style(none)) xscale(noline) xlabel(1 "Means" 2 "Short-Run" 3 "Long-Run") `texts' `goptions'
}
else	{
	twoway `mids' `spikes', legend(`leg') xtitle("") ysize(3) xsize(2) yscale(noline)  yscale(off) xscale(noline) plotregion(style(none)) xlabel(1 "Means" 2 "Short-Run" 3 "Long-Run") `texts' `goptions'
}

* saving:
if "`saving'" != ""	{
	graph export "`saving'.pdf", as(pdf) replace
}
else	{
	graph export "orderplot.pdf", as(pdf) replace
}
restore
end
* -----------------------------------------------------------------------------


